﻿#region Copyrights Notice
//©2009 Microsoft Corporation.  All rights reserved.
#endregion

using System;
using System.Configuration;
using System.DirectoryServices;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;


namespace BitlockerRecovery
{
    class EncryptBitlocker
    {    // Encrypted string
        private const string symmProvider = "symprovider"; 
        static Logger logger ;
        // The file name where the DPAPI-protected symmetric key is stored
        private static readonly string symmKeyFileName = ConfigurationManager.AppSettings["KeyFileName"];

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public byte[] EncryptADValue(string Value)
        {
            try{
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] valueToEncrypt = encoding.GetBytes(Value);

                byte[] ADValue = Cryptographer.EncryptSymmetric(symmProvider, valueToEncrypt);
                return ADValue;

            }
            catch (CryptographicException ex)
            {
                logger = new Logger();
                logger.LogMessage("EncryptADValue/" + Value + "/" + ex.Message );
                throw;
            }
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public byte[] GetADValue(string ldappath, string property, string objectclass)
        {
            try
            {

                string properties = property;
                string searchFilter = "(objectClass=" + objectclass + ")";
                DirectoryEntry objDir = new DirectoryEntry(ldappath, null, null, AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.Signing);
                DirectorySearcher searcher = new DirectorySearcher(objDir, searchFilter);
                searcher.PropertiesToLoad.Add(properties);
                SearchResult sResult = searcher.FindOne();
                byte[] ADValue = new byte[4000];
                if (sResult.Properties.Count > 1 || property != "msTPM-OwnerInformation")
                {
                    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                    byte[] valueToEncrypt = encoding.GetBytes(sResult.Properties[property][0].ToString());
                    ADValue = Cryptographer.EncryptSymmetric(symmProvider, valueToEncrypt);
                }
                return ADValue;

            }
            catch 
            {
                throw;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public bool CheckKey()
        {
            bool KeyExists = false;

            string installedPath = ConfigurationManager.AppSettings["InstallPath"];
            string fileName = Path.Combine(installedPath, symmKeyFileName);
            if (File.Exists(fileName))
            {
                KeyExists = true;
            }
            return KeyExists;
        }
    }
}
